package com.ruoyi.web.controller.system;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.PageResult;
import com.ruoyi.common.core.page.ResponseResult;
import com.ruoyi.system.domain.Withdraw;
import com.ruoyi.system.service.WithdrawService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "管理后台:收支记录")
@RestController
@RequestMapping("admin/money")
public class AdminWithdrawController extends BaseController {
    @Autowired
    private WithdrawService withdrawService;


    /**
     * @param status
     * @return
     * @PreAuthorize("@ss.hasPermi('system:user:edit')")
     * @Log(title = "用户管理", businessType = BusinessType.UPDATE)
     * @PutMapping("/changeStatus")
     */
    @ApiOperation(value = "提现/充值通过")
    @GetMapping("/pass")
    public ResponseResult<List<Withdraw>> pass(Long id) {
        withdrawService.pass(id);
        return this.setSuccessResult();
    }

    @ApiOperation(value = "提现/充值驳回")
    @GetMapping("/cancel")
    public ResponseResult<List<Withdraw>> cancel(Long id) {
        withdrawService.cancel(id);
        return this.setSuccessResult();
    }

    @ApiOperation(value = "提现列表")
    @GetMapping("withdrawList")
    public ResponseResult<List<Withdraw>> withdrawList(Withdraw withdraw) {
        String type = "withdraw";
        PageResult list = this.list(type, withdraw);
        return this.setSuccessResult(list);
    }


    @ApiOperation(value = "充值列表")
    @GetMapping("rechargeList")
    public ResponseResult<List<Withdraw>> rechargeList(Withdraw withdraw) {
        Map<String, Object> map = new HashMap<>();
        String type = "recharge";
        withdraw.setType(type);
        PageResult list = this.list(type, withdraw);
        DateTime thisDay = DateUtil.beginOfDay(new Date());
        withdraw.setStatus("pass");
        BigDecimal todayAmount = withdrawService.sum("money", withdraw, thisDay, new Date());
        BigDecimal yestodayAmount = withdrawService.sum("money", withdraw, DateUtil.offsetDay(thisDay, -1),
                DateUtil.beginOfDay(new Date()));
        BigDecimal totalAmount = withdrawService.sum("money", withdraw, null, null);
        map.put("todayAmount", todayAmount);
        map.put("yestodayAmount", yestodayAmount);
        map.put("totalAmount", totalAmount);
        map.put("pageResult", list);
        return this.setSuccessResult(map);

    }

    private PageResult list(String type, Withdraw withdraw) {

        this.startPage();

        withdraw.setType(type);

        List<Withdraw> list = withdrawService.findList(withdraw);
        PageResult pageResult = this.getPageResult(list);
        return pageResult;
    }


}
